<template>
  <div class="login-page">
    <!-- 背景装饰元素 -->
    <div class="decoration-clouds">
      <div class="cloud cloud-1"></div>
      <div class="cloud cloud-2"></div>
      <div class="cloud cloud-3"></div>
    </div>
    <div class="decoration-rabbits">
      <div class="rabbit rabbit-1"></div>
      <div class="rabbit rabbit-2"></div>
      <div class="rabbit rabbit-3"></div>
    </div>

    <div class="login-container">
      <div class="login-left">
        <!-- 左侧只放置兔子图片，无文字描述 -->
      </div>
      <div class="login-right">
        <div class="login-form-container">
          <h2 class="welcome-text">欢迎回来</h2>
          <p class="login-subtitle">请使用您的账号登录系统</p>

          <el-form
            ref="ruleFormRef"
            :model="ruleForm"
            :rules="rules"
            label-position="top"
            class="login-form"
          >
            <el-form-item label="账号" prop="userName">
              <el-input
                v-model="ruleForm.userName"
                placeholder="请输入您的账号"
                :prefix-icon="User"
                class="custom-input"
              />
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input
                v-model="ruleForm.password"
                show-password
                placeholder="请输入您的密码"
                :prefix-icon="Lock"
                class="custom-input"
              />
            </el-form-item>

            <div class="remember-forgot">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <a href="#" class="forgot-link">忘记密码?</a>
            </div>

            <div class="form-actions">
              <el-button
                type="primary"
                class="login-button"
                @click="submitForm(ruleFormRef)"
                :loading="loading"
              >
                登录系统
              </el-button>
            </div>

            <div class="form-footer">
              <el-button
                text
                class="reset-button"
                @click="resetForm(ruleFormRef)"
              >
                清空输入
              </el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
    <div class="login-footer">
      <p>© 2023 溯源管理系统 版权所有</p>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { useRouter, useRoute } from "vue-router";
import { User, Lock } from "@element-plus/icons-vue";
const router = useRouter();

import httpr from "@/axioslan/axioshttp";

const ruleFormRef = ref<FormInstance>();
const loading = ref(false);
const rememberMe = ref(false);
const ruleForm: any = reactive({
  userName: "",
  password: "",
});

const rules = reactive<FormRules>({
  userName: [{ required: true, message: "账号不能为空", trigger: "blur" }],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
  ],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true;
      httpr({
        url: "/api/User/Login",
        method: "post",
        params: ruleForm,
      })
        .then((res) => {
          loading.value = false;
          if (res.data.code == 200) {
            ElMessage.success(res.data.msg);
            router.push("/menu");

            // 如果选择了记住我，可以存储用户名
            if (rememberMe.value) {
              localStorage.setItem("rememberedUser", ruleForm.userName);
            } else {
              localStorage.removeItem("rememberedUser");
            }

            localStorage.setItem("token", res.data.data.jwtToken);
            localStorage.setItem("userName", res.data.data.userName);
            localStorage.setItem("roleid", res.data.data.roleId);
            localStorage.setItem("departmentId", res.data.data.departmentId);
            localStorage.setItem("leadersName", res.data.data.managerName);

            return;
          } else {
            ElMessage.error(res.data.msg);
            return;
          }
        })
        .catch(() => {
          loading.value = false;
          ElMessage.error("登录失败，请检查网络连接");
        });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

// 页面加载时检查是否有记住的用户名
onMounted(() => {
  const rememberedUser = localStorage.getItem("rememberedUser");
  if (rememberedUser) {
    ruleForm.userName = rememberedUser;
    rememberMe.value = true;
  }
});
</script>

<style scoped>
.login-page {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f0ff 0%, #e6d8ff 100%);
  overflow: hidden;
  position: relative;
}

/* 背景装饰 - 云朵 */
.decoration-clouds {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.cloud {
  position: absolute;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 50%;
  filter: blur(10px);
}

.cloud-1 {
  width: 180px;
  height: 70px;
  top: 15%;
  left: 10%;
  animation: float 15s infinite ease-in-out;
}

.cloud-2 {
  width: 220px;
  height: 80px;
  top: 70%;
  right: 15%;
  animation: float 18s infinite ease-in-out reverse;
}

.cloud-3 {
  width: 150px;
  height: 60px;
  bottom: 20%;
  left: 20%;
  animation: float 12s infinite ease-in-out 2s;
}

/* 背景装饰 - 小兔子 */
.decoration-rabbits {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.rabbit {
  position: absolute;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='rgba(128, 0, 128, 0.2)' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 2C8 2 4 6 4 10a8 8 0 0 0 8 8 8 8 0 0 0 8-8c0-4-4-8-8-8z'%3E%3C/path%3E%3Cpath d='M10 5.5v-1'%3E%3C/path%3E%3Cpath d='M14 5.5v-1'%3E%3C/path%3E%3Cpath d='M9 9h.01'%3E%3C/path%3E%3Cpath d='M15 9h.01'%3E%3C/path%3E%3Cpath d='M12 13c.6 0 1-.4 1-1h-2c0 .6.4 1 1 1z'%3E%3C/path%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-size: contain;
}

.rabbit-1 {
  width: 60px;
  height: 60px;
  top: 20%;
  right: 10%;
  transform: rotate(15deg);
  opacity: 0.6;
}

.rabbit-2 {
  width: 40px;
  height: 40px;
  bottom: 15%;
  right: 25%;
  transform: rotate(-10deg);
  opacity: 0.5;
}

.rabbit-3 {
  width: 50px;
  height: 50px;
  top: 65%;
  left: 8%;
  transform: rotate(5deg);
  opacity: 0.4;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-20px) scale(1.05);
  }
}

.login-container {
  display: flex;
  width: 1000px;
  height: 620px;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.08);
  position: relative;
  margin-bottom: 20px;
  z-index: 1;
  background-color: white;
  backdrop-filter: blur(5px);
}

.login-left {
  flex: 1;
  background-image: url("https://images.pexels.com/photos/6846043/pexels-photo-6846043.jpeg");
  background-size: cover;
  background-position: center;
  position: relative;
  overflow: hidden;
}

/* 紫色滤镜层 */
.login-left::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(128, 0, 128, 0.3);
  z-index: 1;
}

.brand-logo {
  margin-bottom: 30px;
  position: relative;
  z-index: 2;
}

.logo-circle {
  width: 60px;
  height: 60px;
  background-color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.logo-circle span {
  font-size: 30px;
  font-weight: 700;
  color: #2c5282;
}

.brand-content {
  position: relative;
  z-index: 2;
}

.brand-title {
  color: white;
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 16px;
  line-height: 1.2;
}

.brand-description {
  color: rgba(255, 255, 255, 0.8);
  font-size: 18px;
  margin-bottom: 40px;
  line-height: 1.6;
}

.brand-features {
  margin-top: 40px;
}

.feature-item {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.feature-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  color: white;
  font-size: 14px;
}

.feature-text {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
}

.login-right {
  flex: 1;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.login-right::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  width: 100px;
  height: 100px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='rgba(128, 0, 128, 0.1)' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 8.5V5c0-1-1-2-2-2h-6.8M20 15.5V19c0 1-1 2-2 2h-6.8'/%3E%3Cpath d='M4 15.5V19c0 1 1 2 2 2h6.8M4 8.5V5c0-1 1-2 2-2h6.8'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-size: contain;
  opacity: 0.4;
}

.login-form-container {
  width: 80%;
  max-width: 360px;
}

.welcome-text {
  font-size: 28px;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 8px;
}

.login-subtitle {
  color: #718096;
  font-size: 16px;
  margin-bottom: 40px;
}

.login-form {
  width: 100%;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #4a5568;
  padding-bottom: 8px;
  font-size: 14px;
}

:deep(.el-input__wrapper) {
  box-shadow: none;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 0 15px;
  height: 52px;
  transition: all 0.3s;
}

:deep(.el-input__wrapper:hover) {
  border-color: #cbd5e0;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #3182ce;
  box-shadow: 0 0 0 1px #3182ce;
}

:deep(.el-input__inner) {
  height: 50px;
}

:deep(.custom-input .el-input__prefix) {
  margin-left: 4px;
  font-size: 18px;
  color: #718096;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  margin-top: 8px;
}

.forgot-link {
  color: #3182ce;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;
}

.forgot-link:hover {
  color: #2c5282;
  text-decoration: underline;
}

.form-actions {
  margin-top: 16px;
}

.login-button {
  width: 100%;
  height: 52px;
  background: linear-gradient(to right, #8e2de2, #9370db);
  border: none;
  font-size: 16px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
  letter-spacing: 1px;
}

.login-button:hover {
  background: linear-gradient(to right, #7928ca, #8e2de2);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(142, 45, 226, 0.3);
}

.login-button:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(142, 45, 226, 0.3);
}

.form-footer {
  margin-top: 20px;
  text-align: center;
}

.reset-button {
  color: #718096;
  font-size: 14px;
}

.reset-button:hover {
  color: #3182ce;
}

.login-footer {
  margin-top: auto;
  padding: 16px 0;
  color: rgba(128, 0, 128, 0.5);
  font-size: 14px;
  text-align: center;
  position: relative;
  z-index: 1;
}

@media screen and (max-width: 900px) {
  .login-container {
    width: 90%;
    height: auto;
    flex-direction: column;
  }

  .login-left {
    padding: 40px;
    flex: none;
    height: auto;
  }

  .brand-title {
    font-size: 28px;
    margin-bottom: 10px;
  }

  .brand-description {
    font-size: 16px;
    margin-bottom: 20px;
  }

  .brand-features {
    display: none;
  }

  .login-right {
    padding: 40px 0;
  }
}

@media screen and (max-width: 480px) {
  .login-left {
    padding: 30px;
    height: auto;
  }

  .brand-logo {
    margin-bottom: 20px;
  }

  .logo-circle {
    width: 50px;
    height: 50px;
  }

  .logo-circle span {
    font-size: 24px;
  }

  .brand-title {
    font-size: 24px;
  }

  .brand-description {
    font-size: 14px;
  }

  .welcome-text {
    font-size: 24px;
  }

  .login-subtitle {
    font-size: 14px;
    margin-bottom: 30px;
  }

  .login-form-container {
    width: 90%;
  }
}
</style>
